<template>
  <view class="container">
    <!-- header -->
    <view
      style="height: 46px; display: flex; align-items: center; padding: 3px 6px"
    >
      <li-search
        bgColor="#fff"
        placeholder="搜索"
        v-model="keyword"
        :height="36"
        v-on:search="search"
      />
    </view>
    <!-- scroll -->
    <swiper
      class="banner-swiper"
      circular
      autoplay
      indicator-dots
      indicator-color="#aaaaaa"
      indicator-active-color="#e4304d"
      :interval="5000"
      :duration="1000"
    >
      <swiper-item
        class="banner-swiper-item"
        v-for="(item, index) in pageData.AdvCar"
        :key="index"
      >
        <!-- 图片{{ index }} -->
        <image
          :src="
            item.FImgUrl.startsWith('http')
              ? item.FImgUrl
              : imageUrl + item.FImgUrl
          "
          mode="aspectFill"
          @tap="onPicClick(item)"
        ></image>
      </swiper-item>
    </swiper>

    <!-- 分类导航1 -->
    <view class="class-nav">
      <view
        class="class-nav-item"
        v-for="(item, _index) in pageData.ClassNav"
        :key="item.FItemID"
        v-on:tap="onClassNav(item)"
      >
        <!-- <image src="/static/images/icon-home.png" class="class-nav-item-img" /> -->
        <image
          :src="
            item.FImgUrl.startsWith('http')
              ? item.FImgUrl
              : imageUrl + item.FImgUrl
          "
          class="class-nav-item-img"
          mode="aspectFill"
        />
        <text class="class-nav-item-txt"> {{ item.FName }}</text>
      </view>
    </view>

    <!-- 分类导航2 -->
    <view class="class-nav2">
      <view
        class="class-nav2-item"
        v-for="(item, _index) in pageData.AdvPosit"
        :key="item.FItemID"
        v-on:tap="onPicClick(item)"
      >
        <!--       <image
          src="/static/images/icon-home.png"
          class="class-nav2-item-img"
          mode="widthFix"
        /> -->

        <image
          :src="
            item.FImgUrl.startsWith('http')
              ? item.FImgUrl
              : imageUrl + item.FImgUrl
          "
          class="class-nav2-item-img"
          mode="aspectFill"
        />
        <view class="class-nav2-item-txt"> {{ item.FName }}</view>
      </view>
    </view>
  </view>
</template>

<script>
import { reqPost } from "../../common/http.request";
import { useShop } from "../../store/useShop";
import { useUser } from "../../store/useUser";
import {
  uGetStorageSync,
  uNavigateTo,
  uSetStorageSync,
  uSwitchTab,
  uToast,
} from "../../utils/uniUtils";

export default {
  data() {
    return {
      pageData: [],
      isLoaded: false,
      keyword: "",
    };
  },
  async onShow() {
    try {
      /*       if (!uGetStorageSync("hasBindStore")) {
        // uToast("未绑定店铺~", "error");
        return;
      } */
      const accountInfo = uni.getAccountInfoSync();
      if (!uGetStorageSync("systemAppId").length > 0) {
        uni.login({
          success: async (res) => {
            const dt = await reqPost("OnUserLogin", {
              appId: accountInfo.miniProgram.appId,
              code: res.code,
              ...uGetStorageSync("m_store_info"),
            });

            const details = dt.Data[0];
            details &&
              Object.entries(details).forEach((item) => {
                uSetStorageSync(item[0], item[1]);
              });

            if (uGetStorageSync("hasLogin")) {
              useUser().hasLogin = true;
              useShop().GetShoppingCart();
            }
            await this.getPageList();
          },
          fail() {
            console.log("fail");
          },
        });
      } else {
        await this.getPageList();
      }

      this.isLoaded = true;
    } catch (error) {
      console.log("error", error);
      this.isLoaded = false;
    }
  },

  methods: {
    async getPageList() {
      this.pageData = (await reqPost("GetHomeData")).Data;
    },
    onPicClick(item) {
      if (item.FGoodsID > 0) {
        uNavigateTo("/pages/goods/detail?id=" + item.FGoodsID);
      } else if (item.FGoodsClsID > 0) {
        uSetStorageSync("fl", item.FGoodsClsID);
        uSwitchTab("/pages/tab_bar/fl");
      }
    },
    onClassNav(item) {
      if (item.FGoodsID > 0) {
        uNavigateTo("/pages/goods/detail?id=" + item.FGoodsID);
      } else if (item.FGoodsClsID > 0) {
        uSetStorageSync("fl", item.FGoodsClsID);
        uSwitchTab("/pages/tab_bar/fl");
      }
    },
    search() {
      /*  if (!uGetStorageSync("hasBindStore")) {
        uToast("未绑定店铺~", "error");
        return;
      } */

      if (this.keyword.trim().length > 0) {
        uNavigateTo("/pages/goods/search?kw=" + this.keyword);
      } else uToast("请输入关键字~", "error");
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./scss/index.scss";
</style>
